{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d2623dee-249c-4ce9-ab49-b7148ad4b62c",
   "metadata": {},
   "source": [
    "<center><img src=\"./image/CLBLOGO.jpg\" alt=\"创乐博\" style=\"width: 300px;\"/></center>\n",
    "\n",
    "# 6.机器人超声波及红外避障\n",
    "\n",
    "@－－－－湖南创乐博智能科技有限公司－－－－<br>\n",
    "@  文件名：6.机器人超声波及红外避障.py <br>\n",
    "@  版本：V2.0 <br>\n",
    "@  author: zhulin<br>\n",
    "@  说明：机器人超声波及红外避障\n",
    "机器人基本运动！<br>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8fca1605-775c-4ed4-9505-cc0cb291b1a0",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "from LOBOROBOT import LOBOROBOT  # 载入机器人库\n",
    "from gpiozero import DistanceSensor\n",
    "from gpiozero import Button\n",
    "from gpiozero import LED\n",
    "from time import sleep\n",
    "\n",
    "# 超声波模块\n",
    "makerobo_sensor = DistanceSensor(echo=21, trigger=20,max_distance=3, threshold_distance=0.2)\n",
    "\n",
    "Btn  = Button(19,pull_up=True)   # 按键端口\n",
    "Gpin    = LED(5)      # 绿色LED灯接口\n",
    "Rpin    = LED(6)      # 红色LED灯接口\n",
    "\n",
    "\n",
    "SensorRight = Button(16,pull_up=True)     # 右侧红外避障传感器\n",
    "SensorLeft  =  Button(12,pull_up=True)    # 左侧红外避障传感器\n",
    "\n",
    "clbrobot = LOBOROBOT() # 实例化机器人对象\n",
    "        \n",
    "# 按键标志位\n",
    "keyflag = 0\n",
    "\n",
    "def keysacn():\n",
    "    global keyflag\n",
    "    print('*****************************************') \n",
    "    print('* makerobo Button Pressed!*') \n",
    "    print('*****************************************')\n",
    "    Rpin.on()  # 打开红色LED\n",
    "    Gpin.off() # 关闭绿色LED\n",
    "    keyflag = 1   # 按键标志位置1\n",
    "            \n",
    "def released():\n",
    "    print(\"button was released\")\n",
    "    Rpin.off()   # 关闭红色LED\n",
    "    Gpin.on()    # 打开绿色LED\n",
    "\n",
    "# 按键中断函数\n",
    "Btn.when_pressed = keysacn\n",
    "Btn.when_released = released\n",
    "            \n",
    "\n",
    "def loop():\n",
    "        while True:\n",
    "            if keyflag == 1:    \n",
    "                SR_2 = SensorRight.value   \n",
    "                SL_2 = SensorLeft.value    \n",
    "                if SL_2 == 0 and SR_2 ==1:   # 右侧检测到障碍物\n",
    "                    print(\"Left\")\n",
    "                    clbrobot.turnLeft(50,0)\n",
    "                elif SL_2==1 and SR_2 ==0:  # 左侧检测到障碍物\n",
    "                    print(\"Right\")\n",
    "                    clbrobot.turnRight(50,0)\n",
    "                \n",
    "                dis = makerobo_sensor.distance * 100  # 测量距离值，并把m单位换成cm单位\n",
    "                if (dis < 40) == True:\n",
    "                    while (dis < 40) == True:\n",
    "                        clbrobot.t_down(50,0.5)\n",
    "                        clbrobot.turnRight(50,0.1)\n",
    "                        dis = makerobo_sensor.distance * 100\n",
    "                else:\n",
    "                    clbrobot.t_up(50,0)\n",
    "                \n",
    "                print(dis, 'cm')\n",
    "                print('')\n",
    "\n",
    "def destroy():\n",
    "        clbrobot.t_stop(.1)\n",
    "        GPIO.cleanup()\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "        clbrobot.t_stop(.1)\n",
    "        try:\n",
    "                loop()\n",
    "        except KeyboardInterrupt:\n",
    "                destroy()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
